<?php

namespace backend\modules\admin\models;

use Yii;

/**
 * This is the model class for table "auth_item".
 *
 * @property string $name
 * @property int $type
 * @property string|null $description
 * @property string|null $rule_name
 * @property resource|null $data
 * @property int|null $created_at
 * @property int|null $updated_at
 *
 * @property AuthAssignment[] $authAssignments
 * @property AuthRule $ruleName
 * @property AuthItemChild[] $authItemChildren
 * @property AuthItemChild[] $authItemChildren0
 * @property AuthItem[] $children
 * @property AuthItem[] $parents
 */
class AuthItem extends \backend\models\AuthItem
{
    
    /**
     *  @brief 授予指定权限项
     *  
     *  @param [in] items 授权项的名称数组
     *  @return 成功返回true，失败返回false
     */
    public function auth($items){
        // todo loop checking is necessary
        if(empty($items)) return false;
        foreach($items as $item){
            $auth = new AuthItemChild([
                'parent' => $this->name,
                'child' => $item
            ]);
            $auth->save();
        }
        return sizeof($items);
    }
    
    /**
     *  @brief 撤销角色或权限的授权
     *  
     *  @param [in] items 授权项的名称数组
     *  @return 成功返回true，失败返回false
     */
    public function unAuth($items){
        if(empty($items)) return false;
        $deleted = AuthItemChild::deleteAll([
            'parent' => $this->name,
            'child' => $items
        ]);
        return $deleted;
    }
    
}
